package org.example.usercontroller;

import lombok.extern.slf4j.Slf4j;
import org.example.context.BaseContext;
import org.example.pojo.*;
import org.example.pojo.userdto.CustomerLookDTO;
import org.example.pojo.uservo.CustomerLookVO;
import org.example.userservice.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/user/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @DeleteMapping("/delete")
    public Result logoff() {
        Long userid = BaseContext.getCurrentId();
        customerService.delete(userid);
        return Result.success();
    }
    @GetMapping
    public Result findself() {
        return Result.success(customerService.select());
    }
    @GetMapping("/lookthrough")
    public Result lookthrough( @RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "10") int pageSize,
    @RequestParam(value = "name", required = false) String name,
    @RequestParam(value = "price1", required = false) Double price1,
    @RequestParam(value = "price2", required = false) Double price2,
    @RequestParam(value = "part", required = false) int part) {
        CustomerLookDTO customerLookDTO = new CustomerLookDTO();
        customerLookDTO.setPage(page);
        customerLookDTO.setPageSize(pageSize);
        if(customerLookDTO.getPage() == null) {
            customerLookDTO.setPage(1);
        }
        if (customerLookDTO.getPageSize() == null) {
            customerLookDTO.setPageSize(10);
        }
        customerLookDTO.setName(name);
        customerLookDTO.setPrice1(price1);
        customerLookDTO.setPrice2(price2);
        customerLookDTO.setPart(part);
        log.info("customerLookDTO:{}", customerLookDTO);
        PageBean pageBean = customerService.lookthrough(customerLookDTO);
        CustomerLookVO customerLookVO = new CustomerLookVO();
        customerLookVO.setPageBean(pageBean);
        return Result.success(customerLookVO);
    }


}
